package org.windblog.core.entity;

import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class File {

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;

	@Persistent
	private BlobKey blobKey;

	@Persistent
	private String title;

	@Persistent
	private String description;

	@Persistent
	private String author;

	private BlobInfo blobInfo = null;

	public Key getKey() {
		return key;
	}

	public void setKey(Key key) {
		this.key = key;
	}

	public BlobKey getBlobKey() {
		return blobKey;
	}

	public void setBlobKey(BlobKey blobKey) {
		this.blobKey = blobKey;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public BlobInfo getBlobInfo() {
		if (blobInfo != null)
			return blobInfo;
		else {
			BlobInfoFactory bf = new BlobInfoFactory();
			blobInfo = bf.loadBlobInfo(this.getBlobKey());
			return blobInfo;
		}
	}

	public String getFilename()
	{
		return this.getBlobInfo().getFilename();
	}
	
	public String getContentType()
	{
		return this.getBlobInfo().getContentType();
	}
	
	public Date getDate()
	{
		return this.getBlobInfo().getCreation();
	}
	
	public long getSize()
	{
		return this.getBlobInfo().getSize();
	}
	
	public String getEditLink()
	{
		return "/admin/media.jsp?action=edit&filekey="+this.getStrKey();
	}
	
	public String getDeleteLink()
	{
		return "/admin/deletefile?filekey="+this.getStrKey();
	}
	
	public String getViewLink()
	{
		return "/?f="+this.getStrKey();
	}
	
	public String getStrKey()
	{
		return KeyFactory.keyToString(getKey());
	}
	
	public String getDownloadLink()
	{
		return "/downloadfile?filekey="+this.getStrKey();
	}
}